<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tristatemenuitem.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_tristatemenuitem.js.html">tristatemenuitem.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A menu item class that supports three state checkbox semantics.
<a name="line17"></a> *
<a name="line18"></a> * @author eae@google.com (Emil A Eklund)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.TriStateMenuItem&#39;);
<a name="line22"></a>goog.provide(&#39;goog.ui.TriStateMenuItem.State&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line25"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line26"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.ControlContent&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.MenuItem&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.TriStateMenuItemRenderer&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.registry&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Class representing a three state checkbox menu item.
<a name="line36"></a> *
<a name="line37"></a> * @param {goog.ui.ControlContent} content Text caption or DOM structure
<a name="line38"></a> *     to display as the content of the item (use to add icons or styling to
<a name="line39"></a> *     menus).
<a name="line40"></a> * @param {Object=} opt_model Data/model associated with the menu item.
<a name="line41"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper used for
<a name="line42"></a> *     document interactions.
<a name="line43"></a> * @param {goog.ui.MenuItemRenderer=} opt_renderer Optional renderer.
<a name="line44"></a> * @constructor
<a name="line45"></a> * @extends {goog.ui.MenuItem}
<a name="line46"></a> *
<a name="line47"></a> * TODO(attila): Figure out how to better integrate this into the
<a name="line48"></a> * goog.ui.Control state management framework.
<a name="line49"></a> */
<a name="line50"></a>goog.ui.TriStateMenuItem = function(content, opt_model, opt_domHelper,
<a name="line51"></a>    opt_renderer) {
<a name="line52"></a>  goog.ui.MenuItem.call(this, content, opt_model, opt_domHelper,
<a name="line53"></a>      opt_renderer || new goog.ui.TriStateMenuItemRenderer());
<a name="line54"></a>  this.setCheckable(true);
<a name="line55"></a>};
<a name="line56"></a>goog.inherits(goog.ui.TriStateMenuItem, goog.ui.MenuItem);
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Checked states for component.
<a name="line61"></a> * @enum {number}
<a name="line62"></a> */
<a name="line63"></a>goog.ui.TriStateMenuItem.State = {
<a name="line64"></a>  /**
<a name="line65"></a>   * Component is not checked.
<a name="line66"></a>   */
<a name="line67"></a>  NOT_CHECKED: 0,
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * Component is partially checked.
<a name="line71"></a>   */
<a name="line72"></a>  PARTIALLY_CHECKED: 1,
<a name="line73"></a>
<a name="line74"></a>  /**
<a name="line75"></a>   * Component is fully checked.
<a name="line76"></a>   */
<a name="line77"></a>  FULLY_CHECKED: 2
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Menu item&#39;s checked state.
<a name="line83"></a> * @type {goog.ui.TriStateMenuItem.State}
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.ui.TriStateMenuItem.prototype.checkState_ =
<a name="line87"></a>    goog.ui.TriStateMenuItem.State.NOT_CHECKED;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Whether the partial state can be toggled.
<a name="line92"></a> * @type {boolean}
<a name="line93"></a> * @private
<a name="line94"></a> */
<a name="line95"></a>goog.ui.TriStateMenuItem.prototype.allowPartial_ = false;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * @return {goog.ui.TriStateMenuItem.State} The menu item&#39;s check state.
<a name="line100"></a> */
<a name="line101"></a>goog.ui.TriStateMenuItem.prototype.getCheckedState = function() {
<a name="line102"></a>  return this.checkState_;
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Sets the checked state.
<a name="line108"></a> * @param {goog.ui.TriStateMenuItem.State} state The checked state.
<a name="line109"></a> */
<a name="line110"></a>goog.ui.TriStateMenuItem.prototype.setCheckedState = function(state) {
<a name="line111"></a>  this.setCheckedState_(state);
<a name="line112"></a>  this.allowPartial_ =
<a name="line113"></a>      state == goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED;
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Sets the checked state and updates the CSS styling. Dispatches a
<a name="line119"></a> * {@code CHECK} or {@code UNCHECK} event prior to changing the component&#39;s
<a name="line120"></a> * state, which may be caught and canceled to prevent the component from
<a name="line121"></a> * changing state.
<a name="line122"></a> * @param {goog.ui.TriStateMenuItem.State} state The checked state.
<a name="line123"></a> * @private
<a name="line124"></a> */
<a name="line125"></a>goog.ui.TriStateMenuItem.prototype.setCheckedState_ = function(state) {
<a name="line126"></a>  if (this.dispatchEvent(state != goog.ui.TriStateMenuItem.State.NOT_CHECKED ?
<a name="line127"></a>        goog.ui.Component.EventType.CHECK :
<a name="line128"></a>        goog.ui.Component.EventType.UNCHECK)) {
<a name="line129"></a>    this.setState(goog.ui.Component.State.CHECKED,
<a name="line130"></a>        state != goog.ui.TriStateMenuItem.State.NOT_CHECKED);
<a name="line131"></a>    this.checkState_ = state;
<a name="line132"></a>    this.updatedCheckedStateClassNames_();
<a name="line133"></a>  }
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/** @override */
<a name="line138"></a>goog.ui.TriStateMenuItem.prototype.performActionInternal = function(e) {
<a name="line139"></a>  switch (this.getCheckedState()) {
<a name="line140"></a>    case goog.ui.TriStateMenuItem.State.NOT_CHECKED:
<a name="line141"></a>      this.setCheckedState_(this.allowPartial_ ?
<a name="line142"></a>          goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED :
<a name="line143"></a>          goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
<a name="line144"></a>      break;
<a name="line145"></a>    case goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED:
<a name="line146"></a>      this.setCheckedState_(goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
<a name="line147"></a>      break;
<a name="line148"></a>    case goog.ui.TriStateMenuItem.State.FULLY_CHECKED:
<a name="line149"></a>      this.setCheckedState_(goog.ui.TriStateMenuItem.State.NOT_CHECKED);
<a name="line150"></a>      break;
<a name="line151"></a>  }
<a name="line152"></a>
<a name="line153"></a>  var checkboxClass = goog.getCssName(
<a name="line154"></a>      this.getRenderer().getCssClass(), &#39;checkbox&#39;);
<a name="line155"></a>  var clickOnCheckbox = e.target &amp;&amp; goog.dom.classes.has(
<a name="line156"></a>      /** @type {Element} */ (e.target), checkboxClass);
<a name="line157"></a>
<a name="line158"></a>  return this.dispatchEvent(clickOnCheckbox || this.allowPartial_ ?
<a name="line159"></a>      goog.ui.Component.EventType.CHANGE :
<a name="line160"></a>      goog.ui.Component.EventType.ACTION);
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * Updates the extra class names applied to the menu item element.
<a name="line166"></a> * @private
<a name="line167"></a> */
<a name="line168"></a>goog.ui.TriStateMenuItem.prototype.updatedCheckedStateClassNames_ = function() {
<a name="line169"></a>  var renderer = this.getRenderer();
<a name="line170"></a>  renderer.enableExtraClassName(
<a name="line171"></a>      this, goog.getCssName(renderer.getCssClass(), &#39;partially-checked&#39;),
<a name="line172"></a>      this.getCheckedState() ==
<a name="line173"></a>      goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED);
<a name="line174"></a>  renderer.enableExtraClassName(
<a name="line175"></a>      this, goog.getCssName(renderer.getCssClass(), &#39;fully-checked&#39;),
<a name="line176"></a>      this.getCheckedState() == goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>// Register a decorator factory function for goog.ui.TriStateMenuItemRenderer.
<a name="line181"></a>goog.ui.registry.setDecoratorByClassName(
<a name="line182"></a>    goog.ui.TriStateMenuItemRenderer.CSS_CLASS,
<a name="line183"></a>    function() {
<a name="line184"></a>      // TriStateMenuItem defaults to using TriStateMenuItemRenderer.
<a name="line185"></a>      return new goog.ui.TriStateMenuItem(null);
<a name="line186"></a>    });
</pre>


</body>
</html>
